package com.fs.controller;


import com.fs.pojo.*;
import com.fs.service.*;
import com.fs.util.ResponseData;
import com.fs.util.ResponseDataUtil;
import com.fs.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author hua
 * @version 1.0
 * @title LogisticsServlet
 * @description 物流
 * @create 2024/3/4 16:17
 */
@Controller
@RequestMapping("/store")
public class ShopServlet {
    @Resource
    private UserService userService;
    @Resource
    private ProductService productService;


    @RequestMapping("/login")
    @ResponseBody
    public ResponseData login(@RequestBody UserVO userVO, HttpSession session) throws Exception {
        User user = userService.login(userVO);
        if (user == null) {
            return ResponseDataUtil.buildOk(500,"密码或用户名错误");
        } else {
            session.setAttribute("user",user);
            return ResponseDataUtil.buildOk(200,"登录成功");
        }
    }

    @CrossOrigin
    @RequestMapping("/showList")
    @ResponseBody
    public ResponseData<List<Product>> showList(int pageNum) throws Exception {
        Integer size = productService.getTotal(null);
        List<Product> products = productService.showList(null,pageNum);
        return ResponseDataUtil.buildOk(products,size);
    }

    @RequestMapping("/showListByCondition")
    @ResponseBody
    public ResponseData<List<Product>> showListByCondition(String pname,int pageNum) throws Exception {
        Integer size = productService.getTotal(pname);
        List<Product> products = productService.showList(pname,pageNum);
        return ResponseDataUtil.buildOk(products,size);
    }

    @RequestMapping("/showProduct")
    @ResponseBody
    public ResponseData<Product> showProduct(int pid) throws Exception {
        Product product = productService.showProduct(pid);
        return ResponseDataUtil.buildOk(product);
    }

    @RequestMapping("/register")
    @ResponseBody
    public ResponseData register(@RequestBody User user) throws Exception {
        int register = userService.register(user);
        if(register>0) {
            return ResponseDataUtil.buildCreate(null);
        }else {
            return ResponseDataUtil.buildOk("添加失败");
        }
    }
}
